home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / blf082b / config.c < prev    next >
C/C++ Source or Header  |  1993-12-11  |  14KB  |  714 lines

  1. /*
  2.  *    BloufGate
  3.  *    Configuration File And Nodelist 
  4.  *
  5.  *    Public Domain: may be copied and sold freely
  6.  */
  7.  
  8. #include    "blouf.h"
  9.  
  10. /*
  11.  *    find the gatenews record corresponding to a given name
  12.  *
  13.  *    return NULL if conf not gated
  14.  */
  15.  
  16. GATENEWS *area_unix2fido(char *name)
  17. {
  18.     GATENEWS *lastnews;
  19.  
  20.     /* Find fido group */
  21.     lastnews = cf->firstnews;
  22.     while (lastnews != NULL)
  23.     {
  24.         if (strcmp(name, lastnews->news) == 0)
  25.             return lastnews;
  26.  
  27.         lastnews = lastnews->next;
  28.     }
  29.     return NULL;
  30. }
  31.  
  32. GATENEWS *area_fido2unix(char *name)
  33. {
  34.     GATENEWS *lastnews;
  35.  
  36.     /* Find fido group */
  37.     lastnews = cf->firstnews;
  38.     while (lastnews != NULL)
  39.     {
  40.         if (strcmp(name, lastnews->fido) == 0)
  41.         return lastnews;
  42.         
  43.         lastnews = lastnews->next;
  44.     }
  45.     
  46.     return NULL;
  47. }
  48.  
  49. void cleanconfig(void)
  50. {
  51.     GATENEWS *ttt,*lastnews;
  52.     LISTIDX *list,*lll;
  53.     
  54.     lastnews = cf->firstnews;
  55.     while (lastnews != NULL)
  56.     {
  57.         ttt=lastnews;
  58.         lastnews=lastnews->next;
  59.         free(ttt);
  60.     }
  61.     
  62.     list=cf->netlist;
  63.     while(list!=NULL)
  64.     {
  65.         lll=list;
  66.         list=list->next;
  67.         free(lll);
  68.     }
  69.     
  70.     if(cf) free(cf);
  71. }
  72.  
  73. /*
  74.  *    Read config file
  75.  */
  76.  
  77. int readconfig(char *configfile, char *condition)
  78. {
  79.     char    oneline[BLFSTR], temp[BLFSTR];
  80.     FILE    *config;
  81.     int     i, j, k;
  82.     int     error,waitend;
  83.     GATENEWS *lastnews;
  84.  
  85.     cf=malloc(sizeof (BCONFIG));    
  86.     if(cf==NULL)
  87.         return FAIL;
  88.         
  89.      /* init default domain */
  90.     setdomain(NULL);
  91.  
  92.     /* init struct */
  93.     cf->o_zone=-1;
  94.     cf->o_net=-1;
  95.     cf->o_node=-1;
  96.     cf->o_point=-1;
  97.     cf->td_net=-1;
  98.     cf->td_node=-1;
  99.     cf->b_zone=-1;
  100.     cf->b_net=-1;
  101.     cf->b_node=-1;
  102.     cf->outpkt[0]='\0';
  103.     cf->outrfc[0]='\0';
  104.     cf->outnws[0]='\0';
  105.     cf->dupefile[0]='\0';
  106.     cf->tempdir[0]='\0';
  107.     cf->uuname[0]='\0';
  108.     cf->magic[0]='\0';
  109.     cf->fgateann[0]='\0';
  110.     cf->fgatetemp[0]='\0';
  111.     cf->fgatebin[0]='\0';
  112.     cf->fgateann[0]='\0';
  113.     cf->origin[0]='\0';
  114.     cf->maxsize=16000;    /* default 16 KB */
  115.     cf->outmaxsize=cf->maxsize;
  116.     cf->fsc0035=FALSE;
  117.     cf->registered=FALSE;
  118.     cf->fourdpkt=FALSE;
  119.     cf->keep=TRUE;
  120.     cf->nopath=FALSE;
  121.     cf->tunnelwarning=TRUE;
  122.     cf->uucpwarning=FALSE;
  123.     cf->checkdomain=FALSE;
  124.     cf->bitnetwarning=FALSE;
  125.     cf->startrek=FALSE;
  126.     for(i=0;i<PASSLEN;i++) cf->pktpass[i]='\0';
  127.  
  128.     /* init linked lists */
  129.     cf->userlist=NULL;
  130.     cf->netlist=NULL;
  131.     cf->firstnews = NULL;
  132.     
  133.     /* search in current dir */
  134.     config=fopen(configfile, "r");
  135.  
  136.     if(!config)
  137.     {
  138.         char *v;
  139.         v=getenv("BLOUF");
  140.         if(v)
  141.         {
  142.             config = fopen(v,"r");
  143.             if(!config)
  144.             {
  145.                 printf("can't open %s\n",v);
  146.                 return FAIL;
  147.             }
  148.         }
  149.         else
  150.         {
  151.             printf("can't open config\n");
  152.             return FAIL;
  153.         }
  154.     }
  155.  
  156.     error=0;
  157.     waitend=0;
  158.  
  159.     while (!feof(config) && !error)
  160.     {
  161.         if(fgets (oneline, BLFSTR, config)==NULL)
  162.             break;
  163.  
  164.         strip (oneline);
  165.  
  166.         if (oneline[0] != 0)
  167.         {
  168.             /* don't process lines beetwen if <condition>/end  if !our condition*/
  169.             if((i=token("if",oneline))!=0)
  170.             {
  171.                 if(condition)
  172.                 {
  173.                     if(stricmp(condition,&oneline[i]))
  174.                         waitend++; /* not our condition */
  175.                 }
  176.                 else
  177.                     waitend++;
  178.             }
  179.     
  180.             /* if waitend !0 we're in a if/end loop that is not for us */
  181.             if(waitend)
  182.             {
  183.                 if((i=token("end",oneline))!=0)
  184.                     waitend=0;
  185.             }
  186.             else
  187.             {
  188.  
  189.                 if ((i=token("address", oneline))!=0)
  190.                 {
  191.                     j=sscanf (&oneline[i], "%d:%d/%d.%d %d", &cf->o_zone,
  192.                             &cf->o_net, &cf->o_node, &cf->o_point, &i);
  193.                     if(j<4)
  194.                     {
  195.                         printf("invalid address %s\n",oneline+i);
  196.                         error++;
  197.                     }
  198.                     else    if(j==5)
  199.                     {
  200.                         if(cf->o_point==0)
  201.                         {
  202.                             printf("fakenet without point?\n");
  203.                             error++;
  204.                         }
  205.                         else
  206.                         {
  207.                             cf->td_net=i;
  208.                             cf->td_node=cf->o_point;
  209.                         }
  210.                     }
  211.                     else
  212.                     {
  213.                         if(cf->o_point)
  214.                         {
  215.                             printf("point without fakenet?\n");
  216.                             error++;
  217.                         }
  218.                         else
  219.                         {
  220.                             cf->td_net=cf->o_net;
  221.                             cf->td_node=cf->o_node;
  222.                         }
  223.                     }
  224.                 }
  225.                 else if ((i=token("boss", oneline))!=0)
  226.                 {
  227.                     j=sscanf (&oneline[i], "%d:%d/%d", &cf->b_zone,
  228.                             &cf->b_net, &cf->b_node);
  229.                     if(j!=3)
  230.                     {
  231.                         printf("invalid boss %s\n",oneline+i);
  232.                         error++;
  233.                     }
  234.                 }
  235.                 else if ((i=token("outpkt", oneline))!=0)
  236.                 {
  237.                     strcpy (cf->outpkt, &oneline[i]);
  238.                     killslash(cf->outpkt);
  239.                 }
  240.                 else if ((i=token("outrfc", oneline))!=0)
  241.                 {
  242.                     strcpy (cf->outrfc, &oneline[i]);
  243.                     killslash(cf->outrfc);
  244.                 }
  245.                 else if ((i=token("outnews", oneline))!=0)
  246.                 {
  247.                     strcpy (cf->outnws, &oneline[i]);
  248.                     killslash(cf->outnws);
  249.                 }
  250.                 else if ((i=token("dupefile", oneline))!=0)
  251.                 {
  252.                     strcpy (cf->dupefile, &oneline[i]);
  253.                 }
  254.                 else if ((i=token("tempdir", oneline))!=0)
  255.                 {
  256.                     strcpy (cf->tempdir, &oneline[i]);
  257.                     killslash(cf->tempdir);
  258.                 }
  259.                 else if ((i=token("uuname", oneline))!=0)
  260.                 {
  261.                     strcpy (cf->uuname, &oneline[i]);
  262.                     killslash(cf->uuname);
  263.                 }
  264.                 else if ((i=token("domain", oneline))!=0)
  265.                     setdomain(&oneline[i]);
  266.                 else if ((i=token("origin", oneline))!=0)
  267.                 {
  268.                     strcpy (cf->origin, &oneline[i]);
  269.                 }
  270.                 else if ((i=token("reqhelp", oneline))!=0)
  271.                 {
  272.                     strcpy (cf->magic, &oneline[i]);
  273.                 }
  274.                 else if ((i=token("fgateann", oneline))!=0)
  275.                 {
  276.                     strcpy (cf->fgateann, &oneline[i]); /* FGATE */
  277.                 }
  278.                 else if ((i=token("fgateref", oneline))!=0)
  279.                 {
  280.                     strcpy (cf->fgateref, &oneline[i]); /* FGATE */
  281.                     killslash(cf->fgateref);
  282.                 }
  283.                 else if ((i=token("fgatetemp", oneline))!=0)
  284.                 {
  285.                     strcpy (cf->fgatetemp, &oneline[i]); /* FGATE */
  286.                     killslash(cf->fgatetemp);
  287.                 }
  288.                 else if ((i=token("fgatebin", oneline))!=0)
  289.                 {
  290.                     strcpy (cf->fgatebin, &oneline[i]); /* FGATE */
  291.                     killslash(cf->fgatebin);
  292.                 }
  293.                 else if ((i=token("maxsize", oneline))!=0)
  294.                     cf->maxsize = atoi (&oneline[i]);
  295.                 else if ((i=token("outmax", oneline))!=0)
  296.                     cf->outmaxsize = atoi (&oneline[i]);
  297.                 else if ((i=token("logfile", oneline))!=0)
  298.                 {
  299.                     strcpy (temp, &oneline[i]);
  300.                     if(openlog(temp)!=SUCCESS)
  301.                         return FAIL;
  302.                 }
  303.                 else if ((i=token("password", oneline))!=0)
  304.                 {
  305.                     if(strlen(oneline+i)>=PASSLEN)
  306.                     {
  307.                         printf("invalid password.\n");
  308.                         return FAIL;
  309.                     }
  310.                     strcpy (cf->pktpass, &oneline[i]);
  311.                 }                       
  312.                 else if ((i=token("registered", oneline))!=0)
  313.                     cf->registered=TRUE;
  314.                 else if ((i=token("nokeep", oneline))!=0)
  315.                     cf->keep=FALSE;
  316.                 else if ((i=token("fsc-0035", oneline))!=0)
  317.                     cf->fsc0035=TRUE;
  318.                 else if ((i=token("4d-packet", oneline))!=0)
  319.                     cf->fourdpkt=TRUE;
  320.                 else if ((i=token("nopath", oneline))!=0)
  321.                     cf->nopath=TRUE;
  322.                 else if ((i=token("uucpwarning", oneline))!=0)
  323.                     cf->uucpwarning=TRUE;
  324.                 else if ((i=token("bitnetwarning", oneline))!=0)
  325.                     cf->bitnetwarning=TRUE;
  326.                 else if ((i=token("chkdomain", oneline))!=0)
  327.                     cf->checkdomain=TRUE;
  328.                 else if ((i=token("startrek", oneline))!=0)
  329.                     cf->startrek=TRUE;
  330.     
  331.                 else if ((i=token("nodelist", oneline))!=0)
  332.                 {
  333.                     killslash(oneline+i);
  334.                     if(openlist(oneline+i)==FAIL)
  335.                     {
  336.                         logline("!Can't load nodelist");
  337.                         error++;
  338.                     }
  339.                 }
  340.                 else if ((i=token("localusers", oneline))!=0)
  341.                 {
  342.                     killslash(oneline+i);
  343.                     if(openusers(oneline+i)==FAIL)
  344.                     {
  345.                         logline("!Can't load user list");
  346.                         error++;
  347.                     }
  348.                 }
  349.                 else if ((i=token("gatenews", oneline))!=0)
  350.                 {    
  351.                     /* allocate */
  352.                     lastnews = malloc (sizeof(GATENEWS));
  353.                     if(lastnews==NULL)
  354.                     {
  355.                         logline("*Malloc error");
  356.                         return FAIL;
  357.                     }
  358.  
  359.                     /* fill */
  360.                     j = strscan (' ', &oneline[i]);
  361.                     k = strscan ('\t', &oneline[i]);
  362.                     if (j == -1) j = k;
  363.                     else if (k != -1) j = min(j, k);
  364.                     oneline[i+j] = '\0'; j++;
  365.                     while ( ((oneline[i+j] == ' ') || (oneline[i+j] == '\t')) && (oneline[i+j] != '\0'))
  366.                         j++;
  367.     
  368.                     if(oneline[i]=='!')
  369.                     {
  370.                         lastnews->write=FALSE;
  371.                         i++; j--;
  372.                     }
  373.                     else
  374.                         lastnews->write=TRUE;
  375.     
  376.                     if( (strlen(oneline+i+j)>FIDOLEN) || (strlen(oneline+i)>NEWSLEN) )
  377.                     {
  378.                         printf("bad gatenews line: name too long\n");
  379.                         error++;
  380.                         break;
  381.                     }
  382.                     strcpy (lastnews->fido, oneline+i+j);
  383.                     strcpy (lastnews->news, oneline+i);
  384.                     
  385.                     
  386.                     /* link to the beginning of the list */
  387.                     lastnews->next = cf->firstnews;
  388.                     cf->firstnews = lastnews;
  389.                 }
  390.             } /* waitend */
  391.         }
  392. /* fixme, gatefile */
  393.     }
  394.     fclose (config);
  395.  
  396.     
  397.     if(cf->o_zone<=0)
  398.         error++;
  399.     if(cf->o_net<0)
  400.         error++;
  401.     if(cf->o_node<0)
  402.         error++;
  403.     if(cf->o_point<0)
  404.         error++;
  405.     if(cf->td_net<0)
  406.         error++;
  407.     if(cf->td_node<0)
  408.         error++;
  409.     if(cf->b_zone<=0)
  410.         error++;
  411.     if(cf->b_net<0)
  412.         error++;
  413.     if(cf->b_node<0)
  414.         error++;
  415.     if(cf->outpkt[0]=='\0')
  416.         error++;
  417.     if(cf->outrfc[0]=='\0')
  418.         error++;
  419.     if(cf->uuname[0]=='\0')
  420.         error++;
  421.     if(cf->origin[0]=='\0')
  422.         error++;
  423.  
  424.     if(cf->outnws[0]=='\0')
  425.         strcpy(cf->outnws,cf->outrfc);
  426.     
  427.     if(cf->tempdir[0]=='\0')
  428.         strcpy(cf->tempdir,cf->outrfc);
  429.  
  430.     if(error)
  431.     {
  432.         printf("error in config file\n");
  433.         return FAIL;
  434.     }
  435.  
  436.     return SUCCESS;
  437. }
  438.  
  439. /*
  440.  *    Display the config file
  441.  */
  442.  
  443. void displayconfig(void )
  444. {
  445.     USERIDX *list;
  446.     LISTIDX *nli;
  447.     GATENEWS *lastnews;
  448.  
  449.     if(!cf) return;
  450.  
  451.     printf("\n%s version %s Configuration:\n\n",ProgName,Version);
  452.     printf("Gateway (RFC) address : %d:%d/%d.%d (FTN-4D) %d/%d (FTN-2D) %s.%s (RFC)\n",
  453.         cf->o_zone,cf->o_net,cf->o_node,cf->o_point,
  454.         cf->td_net,cf->td_node,
  455.         cf->uuname,finddomain(0,0,0));
  456.     printf("Fidonet (Boss) address:    %d:%d/%d\n",cf->b_zone,cf->b_net,cf->b_node);
  457.     printf("Maximum size limits   : %d bytes (rfc>ftn) %d bytes (ftn>rfc)\n",
  458.         cf->maxsize,cf->outmaxsize);
  459.     printf("Options               : %s %s %s %s %s %s\n",
  460.         cf->checkdomain ? "check-domains" : "no-check-domains",
  461.         cf->registered ? "reg.-site" : "unreg.-site",
  462.         cf->uucpwarning ? "uucp-warn" : "no-uucp-warn",
  463.         cf->bitnetwarning ? "bnet-warn" : "no-bnet-warn",
  464.         cf->startrek ? "galactic" : "terrian",
  465.         cf->fsc0035 ? "use-fsc0035" : "no-fsc0035"); /* fourdpkt */
  466.     printf("Packet password       : %s.\n",cf->pktpass);
  467.     printf("PKT outbound dir.     : %s.\n",cf->outpkt);
  468.     printf("RFC Mail outbound dir.: %s.\n",cf->outrfc);
  469.     printf("RFC news outbound dir.: %s.\n",cf->outnws);
  470.     printf("Duplicates file       : %s.\n",cf->dupefile[0]!=0 ? cf->dupefile : "*NONE*");
  471.     printf("Temporary directory   : %s.\n",cf->tempdir);
  472.     printf("Origin line           : \"%s\"\n",cf->origin);
  473.     printf("\n");
  474.     
  475.     /* list newsgroups */
  476.     lastnews = cf->firstnews;
  477.     while (lastnews != NULL)
  478.     {
  479.         printf("Gate newsgroup %s from/to echomail %s %s\n",
  480.             lastnews->news, lastnews->fido,
  481.             lastnews->write ? "" : "(read only)");
  482.         lastnews = lastnews->next;
  483.     }
  484.  
  485.     /* list users */
  486.     if(cf->userlist)
  487.     {
  488.         printf("USER LIST:\n");
  489.         list=cf->userlist;
  490.         while(list)
  491.         {
  492.             printf("User fido: %s, rfc: %s.\n",list->fido,list->rfc);
  493.             list=list->next;
  494.         }
  495.     }
  496.     else
  497.         printf("No user list\n");
  498.     
  499.     /* net list */
  500.     if(cf->netlist)
  501.     {
  502.         printf("NET LIST:\n");
  503.         nli=cf->netlist;
  504.         while(nli)
  505.         {
  506.             printf("Net %d:%d -> \"%s\"\n",nli->zone,nli->net,nli->name);
  507.             nli=nli->next;
  508.         }
  509.     }
  510.     else
  511.         printf("No net list\n");
  512. }
  513.  
  514. /*
  515.  * Open the userlist
  516.  */
  517.  
  518. int openusers(char *ulist)
  519. {
  520.     FILE *idx;
  521.     int error=0;
  522.     char temp[BLFSTR];
  523.     char *ptr;
  524.     USERIDX *list;
  525.     
  526.     cf->userlist=list=malloc(sizeof(USERIDX));
  527.     if(!list)
  528.     {
  529.         logline("!No memory for net list.");
  530.         return FAIL;
  531.     }
  532.     list->next=NULL;
  533.     
  534.     idx=fopen(ulist,"r");
  535.     if(!idx)
  536.     {
  537.         logline("!Can't load net list!");
  538.         return FAIL;
  539.     }
  540.     
  541.     while(fgets(temp,BLFSTR,idx)!=NULL)
  542.     {
  543.         if(*temp!='#' && *temp!=';')
  544.         {
  545.             clean_string(temp);
  546.             
  547.             ptr=strchr(temp,':');
  548.             if(ptr)
  549.             {    
  550.  
  551.                 *ptr++=0;
  552.                 strcpy(list->fido,temp);
  553.                 if(strlen(ptr)>=BLFSTR)
  554.                     ptr[BLFSTR-1]=0;
  555.                 strcpy(list->rfc,ptr);
  556.                     
  557.                 list->next=malloc(sizeof(USERIDX));
  558.                 list=list->next;
  559.                 if(!list)
  560.                 {
  561.                     logline("Can't malloc user list!");
  562.                     return FAIL;
  563.                 }
  564.                 list->next=NULL;
  565.             }
  566.         }
  567.     }
  568.     
  569.     fclose(idx);
  570.     
  571.     if(error)
  572.     return FAIL;
  573.     
  574.     return SUCCESS;
  575. }
  576.  
  577. /*
  578.  * Get a fidoname user
  579.  */
  580.  
  581. char *finduser(char *in)
  582. {
  583.     USERIDX *list;
  584.     
  585.     if(cf->userlist)
  586.     {
  587.         list=cf->userlist;
  588.         while(list!=NULL)
  589.         {
  590.             if(!stricmp(list->fido,in))
  591.                 return list->rfc;
  592.             list=list->next;
  593.         }
  594.     }
  595.     return NULL;
  596. }
  597.  
  598. /*
  599.  *    Open the nodelist
  600.  */
  601.  
  602. int openlist(char *fnetlist)
  603. {
  604.     FILE *idx;
  605.     int error=0;
  606.     char temp[BLFSTR];
  607.     char *ptr;
  608.     LISTIDX *list;
  609.  
  610.     cf->netlist=list=malloc(sizeof( LISTIDX));
  611.     if(!list)
  612.     {
  613.         logline("!No memory for net list.");
  614.         return FAIL;
  615.     }
  616.     list->next=NULL;
  617.     
  618.     idx=fopen(fnetlist,"r");
  619.     if(!idx)
  620.     {
  621.         logline("!Can't load net list!");
  622.         return FAIL;
  623.     }
  624.  
  625.     while(fgets(temp,BLFSTR,idx)!=NULL)
  626.     {
  627.         if(*temp!='#' && *temp!=';')
  628.         {
  629.             clean_string(temp);
  630.             
  631.             list->zone=atoi(temp);
  632.             ptr=strchr(temp,':');
  633.             if(ptr)
  634.             {    
  635.                  list->net=atoi(++ptr);
  636.                 ptr=strchr(ptr,' ');
  637. /* fixme: be more versatile tab, multispace etc */
  638.                 if(ptr)
  639.                 {
  640.                     if(strlen(ptr)>=BLFSTR-1)
  641.                         ptr[BLFSTR-2]=0;
  642.                     strcpy(list->name,ptr+1);
  643.                     list->next=malloc(sizeof(LISTIDX));
  644.                     list=list->next;
  645.                     if(!list)
  646.                     {
  647.                         logline("Can't malloc net list!");
  648.                         return FAIL;
  649.                     }
  650.                     list->next=NULL;
  651.                 }
  652.             }
  653.         }
  654.     }
  655.  
  656.     fclose(idx);
  657.  
  658.     if(error)
  659.         return FAIL;
  660.  
  661.     return SUCCESS;
  662. }
  663.  
  664. /*
  665.  *    Close the nodelist <fixme: unused?>
  666.  */
  667.  
  668. void closelist(void )
  669. {
  670. }
  671.  
  672. /*
  673.  *    Find a node in the systemlist
  674.  *
  675.  *    returns its system name or NULL if ! found
  676.  */
  677.  
  678. char *findnode(int zone,int net,int node)
  679. {
  680.     LISTIDX *list;
  681.     
  682.     if(!cf->netlist)
  683.         return "FTN BBS -- via gateway";
  684.     else
  685.     {
  686.         list=cf->netlist;
  687.         while(list!=NULL)
  688.         {
  689.             if(list->zone==zone && list->net==net)
  690.                 return list->name;
  691.             list=list->next;
  692.         }
  693.     }
  694.     return NULL;
  695. }
  696.  
  697. /* Domain processor
  698.     IDEA: put that in {node|net}list */
  699.  
  700. static char mydomain[BLFSTR];
  701.  
  702. void setdomain(char *domain)
  703. {
  704.     if(domain)
  705.         strcpy(mydomain,domain);
  706.     else
  707.         strcpy(mydomain,DEFAULT_DOMAIN);
  708. }
  709.  
  710. char *finddomain(int zone, int net, int node)
  711. {
  712.     return mydomain;
  713. }
  714.